package com.example.laixunlong;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

/*按钮事件继承自所有控件事件*/
public class ButtonEvent_explain extends AppCompatActivity implements View.OnClickListener
{
    TextView txt;
    Button but1;
    Button but2;
    Button but3;
    Button but4;
    Button but5;
    Button but6;
    Button but7;
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buttonevent_explain);//页面初始化语句,需保持在所有变量初始化执之前
        but1 = findViewById(R.id.but_event_1);
        but2 = findViewById(R.id.but_event_2);
        but3 = findViewById(R.id.but_event_3);
        but4 = findViewById(R.id.but_event_4);
        but5 = findViewById(R.id.but_event_5);
        but6 = findViewById(R.id.but_event_6);
        but7 = findViewById(R.id.but_event_7);
        txt=findViewById(R.id.event_txt);


    //事件监听方式1:直接通过new一个内部类的方式创建监听
        /*点击事件*/
        but1.setOnClickListener(new View.OnClickListener() {
            @Override
            //事件中的参数必须是view,因为Event继承于view,通过该变量将获取触发页面的几乎所有信息
            public void onClick(View v) {
                Log.e("事件", "点击触发");
            }
        });
        /*长按事件*/
        but3.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Log.e("事件", "长按触发");
                return false;
            }
        });
        /*触摸事件*/
        but5.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                Log.e("事件", "触摸触发");
                return false;
            }
        });

    //事件监听方式2:通过静态对象的方式创建监听,此方式可防止当事件过多时导致内存泄漏
        /*点击事件*/
        but2.setOnClickListener(new inEvent_click(txt,but7));
        /*长按事件*/
        but4.setOnLongClickListener(new inEvent_longclick(txt));
        /*触摸事件*/
        but6.setOnTouchListener(new inEvent_touch(txt));



    //事件监听方式3:全局监听,id过滤
        but7.setOnClickListener(this);
    }
    /**/@Override
    public void onClick(View v) {
        if(v.getId()==R.id.but_event_7)
        {
            txt.setText("触发点击事件:"+((Button)v).getText());
            Log.e("事件", "点击触发");
        }
    }





    /*点击事件内部类*/
    static class inEvent_click implements View.OnClickListener
    {
        private TextView id;
        private Button bt;
        inEvent_click(TextView a,Button b)
        {
            this.id=a;
            this.bt=b;
        }
        @Override
        public void onClick(View v) {//将会把按钮名称替换到文本上
            Log.e("事件", "点击触发");
            bt.setEnabled(false);
            id.setText("触发点击事件:"+ ((TextView)v).getText());
        }
    }
    /*长按事件内部类*/
    static class inEvent_longclick implements View.OnLongClickListener
    {
        private TextView txt;
        inEvent_longclick(TextView t)
        {
            txt=t;
        }
        @Override
        public boolean onLongClick(View v) {
            Log.e("事件", "长按触发");
            txt.setText("触发长按事件:"+((TextView)v).getText());
            return false;
        }
    }
    /*触摸事件内部类*/
    static class inEvent_touch implements View.OnTouchListener
    {
        private TextView txt;
        inEvent_touch(TextView t)
        {
            txt=t;
        }
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.e("事件", "触摸触发");
            txt.setText("触发触摸事件:"+((TextView)v).getText());
            return false;
        }
    }
}
